package com.shuaiche.yeguan.ui.map;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.core.ServiceSettings;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.SCApplication;
import com.shuaiche.yeguan.model.ChangeLocation;
import com.shuaiche.yeguan.model.SCAddressParamsModel;
import com.shuaiche.yeguan.util.LogUtils;
import com.shuaiche.yeguan.util.ResourceUtils;
import com.shuaiche.yeguan.util.StringUtils;
import com.shuaiche.yeguan.util.ToastShowUtils;
import com.shuaiche.yeguan.util.permission.MPermission;
import com.shuaiche.yeguan.util.permission.OnMPermissionDenied;
import com.shuaiche.yeguan.util.permission.OnMPermissionGranted;
import com.shuaiche.yeguan.util.permission.OnMPermissionNeverAskAgain;
import com.shuaiche.yeguan.views.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class ChooseAddressMapActivity extends AppCompatActivity implements AMapLocationListener, AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, GeocodeSearch.OnGeocodeSearchListener, View.OnClickListener {

    private static final int BASIC_PERMISSION_REQUEST_CODE_FOR_LOCATION = 1002;  //定位权限

    MapView mapView;
    EditText etInputSearch;
    RecyclerView rvSearchList;
    ImageButton ibLocation;
    RelativeLayout rlMap;
    FrameLayout flInputSearch;
    RecyclerView inputSearchRecyclerView;
    TextView tvCancel;
    TextView noInputSearchHint;
    TextView noLocationSearchHint;

    private AMap aMap;
    private MyLocationStyle myLocationStyle;
    private AMapLocationClient aMapLocationClient = null;
    private AMapLocationClientOption mLocationOption = null;
    private AMapLocationListener aMapLocationListener;

    // 定位的经纬度
    private LatLng locationLatLng;
    // 地图移动过后的经纬度
    private LatLng target;
    private GeocodeSearch geocodeSearch;
    private Marker mPositionMark;

    // 逆编码地理 地址信息
    private SCAddressParamsModel addressParams;
    private RegeocodeAddress regeocodeAddress;
    private RegeocodeQuery regeocodeQuery;

    // 兴趣点检索
    private PoiSearch.Query query;
    private int currentPage = 0;
    private PoiSearch poiSearch;
    private PoiResult result;
    private int poiResultPageCount;
    private ArrayList<PoiItem> poiItems;

    // 城市编码
    private String cityCode;
    private String city;
    private double latitude;
    private double longitude;
    private String locationDistrict;
    private String locationProvince;
    private String locationCity;
    private String locationAddress;
    private ArrayList<ChangeLocation> nearByList;
    private ArrayList<ChangeLocation> inputList;
    private SearchNearByAddressAdapter addressAdapter;
    private SearchNearByAddressAdapter inputSearchAdapter;

    // 需要传递回去的location
    private ChangeLocation mTransferLocation = new ChangeLocation();

    private Double lat;
    private Double lon;
    private LatLng latLng;
    private boolean hasLocation;

    private RelativeLayout rl_back;
    private TextView tv_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_choose_address_map);

        getData();
        initViews();
        mapView.onCreate(savedInstanceState);
    }

    private void initViews() {
        initFindviews();

        MapsInitializer.updatePrivacyShow(this,true,true);
        MapsInitializer.updatePrivacyAgree(this,true);
        ServiceSettings.updatePrivacyShow(this,true,true);
        ServiceSettings.updatePrivacyAgree(this,true);

        initMapview();

        initEditText();
        initRecyclerView();
    }

    private void getData() {
        if (getIntent() != null) {
            lat = getIntent().getDoubleExtra("lat",0f);
            lon = getIntent().getDoubleExtra("lon",0f);

            cityCode = getIntent().getStringExtra("cityCode");
            hasLocation = getIntent().getBooleanExtra("hasLocation", false);
            if (lat != 0f && lon != 0f) {
                latLng = new LatLng(lat, lon);
                latitude = lat;
                longitude = lon;
            }
        }
    }

    private void initMapview() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//                SCConfirmDialogFragment dialog = new SCConfirmDialogFragment();
//                dialog.addValues(SCConfirmDialogFragment.TITLE, "申请定位权限");
//                dialog.addValues(SCConfirmDialogFragment.CONTENT, "申请定位权限，获取您当前的位置信息完成定位，设置您的公司位置信息");
//                dialog.addValues(SCConfirmDialogFragment.CONFIRM, "申请权限");
//                dialog.addValues(SCConfirmDialogFragment.CANCEL, "拒绝");
//                dialog.commitAddValues();
//                dialog.showAllowingStateLoss(getCurrContext());
//                dialog.setConfirmListener(new SCConfirmDialogFragment.ConfirmListener() {
//                    @Override
//                    public void confirm() {

//                        dialog.dismiss();
//
//                    }
//
//                    @Override
//                    public void cancel() {
//                        dialog.dismiss();
//
//                    }
//                });

                showCustomDialog();

            }else {

                initMap();
            }
        }
    }

    private void showCustomDialog() {
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_permission);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(false);
        Button btnApply = dialog.findViewById(R.id.btn_apply);
        Button btnReject = dialog.findViewById(R.id.btn_reject);

        btnApply.setOnClickListener(v -> {
            MPermission.with(ChooseAddressMapActivity.this)
                    .setRequestCode(BASIC_PERMISSION_REQUEST_CODE_FOR_LOCATION)
                    .permissions(Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION)
                    .request();
            // 申请定位权限
            dialog.dismiss();
        });

        btnReject.setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.show();
    }
    @OnMPermissionGranted(BASIC_PERMISSION_REQUEST_CODE_FOR_LOCATION)
    public void onBasicPermissionSuccess() {
        initMap();
    }

    @OnMPermissionDenied(BASIC_PERMISSION_REQUEST_CODE_FOR_LOCATION)
    @OnMPermissionNeverAskAgain(BASIC_PERMISSION_REQUEST_CODE_FOR_LOCATION)
    public void onBasicPermissionFailed() {
        ToastShowUtils.showTipToast("请先赋予app定位权限");
    }




    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvSearchList.setLayoutManager(manager);
        rvSearchList.addItemDecoration(
                new SpacesItemDecoration(0, ResourceUtils.dipToPx(this, 1),
                        ResourceUtils.getColor(this, R.color.backgroundColor)));
        addressAdapter = new SearchNearByAddressAdapter(this, nearByList);
        rvSearchList.setAdapter(addressAdapter);

        LinearLayoutManager manager1 = new LinearLayoutManager(this);
        inputSearchRecyclerView.setLayoutManager(manager1);
        inputSearchRecyclerView.addItemDecoration(
                new SpacesItemDecoration(0, ResourceUtils.dipToPx(this, 1),
                        ResourceUtils.getColor(this, R.color.backgroundColor)));
        inputSearchAdapter = new SearchNearByAddressAdapter(this, nearByList);
        inputSearchRecyclerView.setAdapter(inputSearchAdapter);

    }

    private void initFindviews() {
        mapView = findViewById(R.id.choose_map_view);
        etInputSearch = findViewById(R.id.et_input_search);
        rvSearchList = findViewById(R.id.rv_search_list);
        ibLocation = findViewById(R.id.ib_location);
        rlMap = findViewById(R.id.rl_map);
        flInputSearch = findViewById(R.id.fl_input_search_list);
        inputSearchRecyclerView = findViewById(R.id.rv_input_search_list);
        tvCancel = findViewById(R.id.tv_cancel);
        noInputSearchHint = findViewById(R.id.no_result_hint);
        noLocationSearchHint = findViewById(R.id.no_location_result_hint);

        rl_back = findViewById(R.id.rl_back);
        tv_confirm = findViewById(R.id.tv_confirm);

        tvCancel.setOnClickListener(this);
        ibLocation.setOnClickListener(this);
        rl_back.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_location:
                reLocation();
                break;
            case R.id.tv_cancel:
                etInputSearch.setText("");
                etInputSearch.setFocusable(false);
                etInputSearch.setFocusableInTouchMode(true);
                flInputSearch.setVisibility(View.GONE);
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_confirm:
                postAddress(mTransferLocation);
                break;
        }
    }

    /**
     * 定位icon的点击事件
     */
    private void reLocation() {
        if (aMap != null && locationLatLng != null) {
            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 18));
        }
    }


    private void initEditText() {
        etInputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (nearByList != null && nearByList.size() > 0) {
                    nearByList.clear();
                }
                String newText = s.toString().trim();

                if (!StringUtils.isEmpty(newText)) {
                    InputtipsQuery inputtipsQuery = new InputtipsQuery(newText, "");
//                    inputtipsQuery.setCityLimit(true);

                    Inputtips inputtips = new Inputtips(ChooseAddressMapActivity.this, inputtipsQuery);
                    inputtips.setInputtipsListener(new Inputtips.InputtipsListener() {
                        @Override
                        public void onGetInputtips(List<Tip> list, int rCode) {
                            if (rCode == AMapException.CODE_AMAP_SUCCESS && list.size() > 0) {
                                inputList = new ArrayList<>();
                                for (int i = 0; i < list.size(); i++) {
                                    ChangeLocation location = new ChangeLocation();
                                    location.setName(list.get(i).getName());
                                    location.setAddress(list.get(i).getAddress());
                                    // getDistrict() = 浙江省 杭州市 拱墅区
                                    location.setDistrict(list.get(i).getDistrict());
                                    location.setLatitude(list.get(i).getPoint().getLatitude());
                                    location.setLongitude(list.get(i).getPoint().getLongitude());
                                    location.setAdCode(list.get(i).getAdcode());
                                    inputList.add(location);
                                }

                                inputSearchAdapter.setNewData(inputList);
                            } else {
//                                LogUtils.e(TAG, "InputtipsQuery: ErrorCode = " + rCode);
                            }
                        }
                    });

                    inputtips.requestInputtipsAsyn();
                } else {
                    getNearBy(latitude, longitude);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputWord = s.toString().trim();
                if (!StringUtils.isEmpty(inputWord)) {
                    inputSearchAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
                        @Override
                        public void onItemClick(View view, int i) {
                            if (inputList != null && inputList.size() > 0) {
                                String name = inputList.get(i).name;
                                String itemAddress = inputList.get(i).address;
                                ChangeLocation location = inputList.get(i);

                                postAddress(location);
                            }
                        }
                    });
                } else {
                    getNearBy(latitude, longitude);
                }
            }
        });

        etInputSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    flInputSearch.setVisibility(View.VISIBLE);
                    tvCancel.setVisibility(View.VISIBLE);
                } else {
                    flInputSearch.setVisibility(View.GONE);
                    tvCancel.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        initLocationStyle();
        initLocationListener();
        aMap.setOnMapLoadedListener(this);
        aMap.setOnCameraChangeListener(this);

    }

    private void initLocationStyle() {
        //初始化定位蓝点样式类
        myLocationStyle = new MyLocationStyle();
        //定位一次，且将视角移动到地图中心点。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
        //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
//        myLocationStyle.interval(3000);
        //方法自5.1.0版本后支持
        //设置是否显示定位小蓝点，用于满足只想使用定位，不想使用定位小蓝点的场景，
        // 设置false以后图面上不再有定位蓝点的概念，但是会持续回调位置信息。
        myLocationStyle.showMyLocation(true);
        // 不显示蓝色的范围圆圈
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色
        //设置定位蓝点的Style
        aMap.setMyLocationStyle(myLocationStyle);
        // 设置默认定位按钮是否显示，非必需设置。
        aMap.getUiSettings().setMyLocationButtonEnabled(false);
        aMap.getUiSettings().setZoomControlsEnabled(false);
        // 倾斜手势：是否可以双指倾斜
        aMap.getUiSettings().setTiltGesturesEnabled(false);
        // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        aMap.setMyLocationEnabled(true);
    }

    private void initLocationListener() {
        try {
            aMapLocationClient = new AMapLocationClient(getApplicationContext());
            if (aMapLocationClient == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果
        mLocationOption.setOnceLocation(true);
        // 且该结果一定来自与设备环境相符合的AP源
        mLocationOption.setOnceLocationLatest(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置定位参数
        aMapLocationClient.setLocationOption(mLocationOption);
        //设置定位回调监听
        aMapLocationClient.setLocationListener(this);
        //启动定位
        aMapLocationClient.startLocation();
    }

    @Override
    public void onMapLoaded() {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.setFlat(true);
        markerOptions.anchor(0.5f, 0.5f);
        markerOptions.position(new LatLng(0, 0));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_map_marker)));
        mPositionMark = aMap.addMarker(markerOptions);

        if (latLng != null) {
            moveToLocationed();
        } else {
            if (locationLatLng != null) {
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 18));
            } else {
                aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
            }
        }

        mPositionMark.showInfoWindow();
        //主动显示infoWindow 
        mPositionMark.setPositionByPixels(mapView.getWidth() / 2, rlMap.getHeight() / 2);
    }

    /**
     * 移动到指定地点
     */
    private void moveToLocationed() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (aMap != null && latLng != null) {
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
                    getNearBy(lat, lon);
                }
            }
        }, 900);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (aMap != null && latLng != null) {
//                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
                    getNearBy(lat, lon);
                }
            }
        }, 1000);

    }

    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        //这个target 就是地图移动过后中心点的经纬度
        target = cameraPosition.target;
        //这个方法是逆地理编码解析出详细位置
        Geo(target);
    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {
        latitude = cameraPosition.target.latitude;
        longitude = cameraPosition.target.longitude;
        Geo(cameraPosition.target);
    }

    /**
     * 先要执行逆地理编码的搜索
     *
     * @param latlng 逆地理编码
     */
    public void Geo(LatLng latlng) {
        try {
            geocodeSearch = new GeocodeSearch(this);
        } catch (AMapException e) {
            e.printStackTrace();
        }
        geocodeSearch.setOnGeocodeSearchListener(this);
        //和上面一样
        // 第一个参数表示一个Latlng(经纬度)，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(latlng.latitude, latlng.longitude), 200, GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);

    }

    private void recordLocation() {
        mTransferLocation.setAddress(regeocodeAddress.getFormatAddress());
        mTransferLocation.setLatitude(regeocodeQuery.getPoint().getLatitude());
        mTransferLocation.setLongitude(regeocodeQuery.getPoint().getLongitude());

        mTransferLocation.setProvince(regeocodeAddress.getProvince());
        mTransferLocation.setCity(regeocodeAddress.getCity());
        mTransferLocation.setDistrict(regeocodeAddress.getDistrict());
        mTransferLocation.setAdCode(regeocodeAddress.getAdCode());
        mTransferLocation.setCityCode(regeocodeAddress.getCityCode());
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        // 如果逆地理编码成功，就获取到中心点的详细位置，并且在TextView中进行显示，就如同一开始的那张图片所示。
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            if (regeocodeResult != null) {
                regeocodeAddress = regeocodeResult.getRegeocodeAddress();
                regeocodeQuery = regeocodeResult.getRegeocodeQuery();
                cityCode = regeocodeAddress.getCityCode();
                city = regeocodeAddress.getCity();

                recordLocation();
                getNearBy(latitude, longitude);
            }
        } else {
            LogUtils.log("onRegeocodeSearched", "onRegeocodeSearched Error:" + i);
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        LogUtils.log("onLocationChanged","onLocationChanged");
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //可在其中解析amapLocation获取相应内容。
                locationLatLng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                cityCode = aMapLocation.getCityCode();
                city = aMapLocation.getCity();
//                latitude = aMapLocation.getLatitude();
//                longitude = aMapLocation.getLongitude();
                locationDistrict = aMapLocation.getDistrict();
                locationProvince = aMapLocation.getProvince();
                locationCity = aMapLocation.getCity();
//                locationAddress = aMapLocation.getAddress();
                locationAddress = aMapLocation.getPoiName();

                getNearBy(latitude, longitude);
                if (latLng != null) {
                    moveToLocationed();
                }
            } else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                LogUtils.log("onLocationChanged", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }

    /**
     * 获取定位附近的地址信息
     */
    private void getNearBy(double lon, double lng) {
        initPoiSearch(lon, lng);
        addressAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                if (nearByList != null && nearByList.size() > 0) {
                    String name = nearByList.get(i).name;
                    String nearBy = nearByList.get(i).address;
                    ChangeLocation location = nearByList.get(i);
                    postAddress(location);
                }
            }
        });
    }

    private void postAddress(ChangeLocation location) {
//        if (StringUtils.isEmpty(location.getAddress())) {
//            ToastShowUtils.showTipToast("地址不能为空");
//        } else {
        addressParams = new SCAddressParamsModel();
        addressParams.setLocationAddTitle(location.getName());
        if (!StringUtils.isEmpty(location.getAddress())) {
            addressParams.setDetailAddress(location.getAddress());
        } else {
            addressParams.setDetailAddress(location.getDistrict() + location.getName());
        }
        addressParams.setLatitude(location.getLatitude());
        addressParams.setLongitude(location.getLongitude());

        addressParams.setProvince(location.getProvince());
        addressParams.setCity(location.getCity());
        addressParams.setAdName(location.getAdName());
        addressParams.setDistrict(location.getDistrict());

        addressParams.setAdCode(location.getAdCode());
        addressParams.setCityCode(location.getCityCode());
        goBackPrevious();
//        }
    }

    private void goBackPrevious() {
//        Intent intent = new Intent();
//        intent.putExtra("address", addressParams);
//        setResult(Activity.RESULT_OK, intent);
//        finish();

        ReactContext reactContext = SCApplication.getApplication().getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
        if (reactContext == null) {
            return;
        }
        WritableMap event = Arguments.createMap();
        //传递的参数
        event.putString("address", addressParams.getDetailAddress());
        event.putDouble("lat", addressParams.getLatitude());
        event.putDouble("lon", addressParams.getLongitude());
        reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("selectMapResult",event);

        finish();
    }

    /**
     * 兴趣检索
     */
    private void initPoiSearch(double lon, double lng) {
        // 1、关键字未设置城市信息（默认为全国搜索）时，如果涉及多个城市数据返回，仅会返回建议城市，请根据APP需求，选取城市进行搜索。
        // 2、不设置POI的类别，默认返回“餐饮服务”、“商务住宅”、“生活服务”这三种类别的POI，下方提供了POI分类码表，请按照列表内容设置希望检索的POI类型。（建议使用POI类型的代码进行检索）
        //s表示搜索字符串，
        //s1参数表示POI搜索类型，二者选填其一，选用POI搜索类型时建议填写类型代码，码表可以参考下方（而非文字）
        //s2表示POI搜索区域，可以是城市编码也可以是城市名称，也可以传空字符串，空字符串代表全国在全国范围内进行搜索

        if (cityCode != null) {
            query = new PoiSearch.Query("", "170200|170201|170202|170203|170204|170205|170206|170207|170208|170209", cityCode);
            query.setPageSize(30);
            query.setPageNum(currentPage);
            try {
                poiSearch = new PoiSearch(this, query);
            } catch (AMapException e) {
                e.printStackTrace();
            }
            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                @Override
                public void onPoiSearched(PoiResult poiResult, int rCode) {
                    if (rCode == 1000 && poiResult != null && poiResult.getPois().size() > 0) {
                        ArrayList<PoiItem> pois = poiResult.getPois();
                        nearByList = new ArrayList<>();
                        if (pois.size() > 0) {
                            ChangeLocation firstLocation = new ChangeLocation();
                            firstLocation.setFirstItem(true);
                            firstLocation.setLatitude(locationLatLng.latitude);
                            firstLocation.setLongitude(locationLatLng.longitude);
                            firstLocation.setProvince(locationProvince);
                            firstLocation.setCity(locationCity);
                            firstLocation.setAdName(locationDistrict);
                            firstLocation.setName(locationAddress);
                            firstLocation.setAddress(locationAddress);
                            nearByList.add(firstLocation);
                        }
                        for (int i = 0; i < pois.size(); i++) {
                            ChangeLocation location = new ChangeLocation();
                            location.setName(pois.get(i).getTitle());
                            location.setAddress(pois.get(i).getSnippet());
                            location.setLatitude(pois.get(i).getLatLonPoint().getLatitude());
                            location.setLongitude(pois.get(i).getLatLonPoint().getLongitude());
                            // getProvinceName() = 浙江省
                            location.setProvince(pois.get(i).getProvinceName());
                            // getCityName() = 杭州市
                            location.setCity(pois.get(i).getCityName());
                            // getAdName() = 拱墅区
                            location.setAdName(pois.get(i).getAdName());
                            location.setAdCode(pois.get(i).getAdCode());
                            nearByList.add(location);
                        }
                        addressAdapter.setNewData(nearByList);
                        aMapLocationClient.stopLocation();
                    } else {
//                        LogUtils.e(TAG, "搜索附近失败");
                    }
                }

                @Override
                public void onPoiItemSearched(PoiItem poiItem, int i) {

                }
            });
            // 调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。
            poiSearch.searchPOIAsyn();
            // 设置周边搜索的中心点以及半径
            poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(lon, lng), 2000));
        }/* else {
            ToastShowUtils.showTipToast("定位失败，无法获取附近位置");
        }*/
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (aMapLocationClient != null) {
            aMapLocationClient.stopLocation();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (aMapLocationClient != null) {
            aMapLocationClient.onDestroy();
        }
        if (aMap != null) {
            aMap.setMyLocationEnabled(false);
        }
        if (mapView != null) {
            mapView.onDestroy();
        }
        aMap = null;
    }


}